package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.mapper.ItemMapper;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Item;
import com.guoq.pojo.ItemExample;
import com.guoq.pojo.ItemExample.Criteria;
import com.guoq.pojo.Result;
import com.guoq.service.IItemService;

@Service("itemService")
public class ItemServiceImpl implements IItemService {

	@Resource
	private ItemMapper itemMapper;

	/**
	 * 根据id查询商品信息
	 */
	@Override
	public Item getItemById(int id) {
		// TODO Auto-generated method stub
		return this.itemMapper.selectByPrimaryKey(id);
	}

	/**
	 * 获取不分页列表
	 */
	@Override
	public List<Item> getItemList() {
		// TODO Auto-generated method stub
		ItemExample example = new ItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte) 0);//正常
		example.setOrderByClause("id");
		List<Item> list = itemMapper.selectByExample(example);
		return list;
	}

	/**
	 * 获取分页列表
	 */
	@Override
	public EUDataGridResult getItemList(Integer page, Integer rows, String sid) {
		
		ItemExample example = new ItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andSmenuidEqualTo(Integer.parseInt(sid));
		//分页处理
		PageHelper.startPage(page, rows);
		List<Item> list = itemMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Item> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public List<Item> selectByExample(ItemExample example) {
		// TODO Auto-generated method stub
		List<Item> list = itemMapper.selectByExample(example);
		return list;
	}

	@Override
	public Item selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Item item = itemMapper.selectByPrimaryKey(id);
		return item;
	}

	@Override
	public int countByExample(ItemExample example) {
		// TODO Auto-generated method stub
		int count = itemMapper.countByExample(example);
		return count;
	}

	@Override
	public Result createItem(Item item) throws Exception {

		Result result = this.insertItem(item);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insertItem(Item item){
		
		item.setStatus((byte) 0);
		item.setCreatetime(new Date());
		item.setUpdatetime(new Date());
		itemMapper.insertSelective(item);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Item record) throws Exception {

		Result result = null;
		
		record.setStatus((byte) 2);
		record.setUpdatetime(new Date());
		int num = itemMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result updateItem(Item record) throws Exception {

		Result result = null;
		int num = itemMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

}
